import * as service from '../services/publicNumber';


const myDate = new Date();
const date = `${myDate.getFullYear()}${myDate.getMonth() + 1}${myDate.getDate()}`;
export default {
  namespace: 'publicNumber',
  state: {
    DimensionCode: {
      cardUsers: [
        {
          title: '新关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '取消关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,

        }, {
          title: '净增关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '总关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
      ],
      cardMsg: [
        {
          title: '消息发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '消息发送人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '人均发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },

      ],
    },
    Happylife: {
      cardUsers: [
        {
          title: '新关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '取消关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,

        }, {
          title: '净增关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '总关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
      ],
      cardMsg: [
        {
          title: '消息发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '消息发送人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '人均发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },

      ],
    },
    Union: {
      cardUsers: [
        {
          title: '新关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '取消关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,

        }, {
          title: '净增关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '总关注人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
      ],
      cardMsg: [
        {
          title: '消息发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '消息发送人数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },
        {
          title: '人均发送次数',
          all: 0,
          day: 0,
          week: 0,
          month: 0,
        },

      ],
    },
  },
  reducers: {
    save(state, { payload: { data } }) {
      return { ...state, ...data };
    },
  },
  effects: {
    *fetch({ payload: { pathname, dateString } }, { call, put }) {
      const day1 = new Date();
      day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
      const date = dateString || `${day1.getFullYear()}-${day1.getMonth() + 1}-${day1.getDate()}`;
      try {
        const { data } = yield call(service.fetch, date);
        yield put({ type: 'save', payload: { data } });
      } catch (err) {
        console.log('x');
        console.log(err);
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        // if (pathname == "/publicNumber"&& data!=null) {
        //   dispatch({ type: 'save', payload: { data} });
        // }else
        if (pathname == '/publicNumber') {
          dispatch({ type: 'fetch', payload: { ...query, pathname } });
          // dispatch({ type: 'save', payload: { data} });
        }
      });
    },
  },
};
